var _inherit = require('core/inherit.js');

function Anima() {
    this.mFinish = true;
    this.mRemain = 0;
}
Anima.prototype.update = function (stride) {
    this.mFinish = true;
    this.mRemain = stride;
};
Anima.prototype.isFinish = function () {
    return this.mFinish;
};
Anima.prototype.reset = function () {
    this.mFinish = true;
    this.mRemain = 0;
};
Anima.prototype.getRemain = function () {
    return this.mRemain;
};

function AnimaGroup() {
    Anima.call(this);
    this.mAnimas = [];
    this.mIndex = 0;
}
_inherit(AnimaGroup, Anima);
AnimaGroup.prototype.update = function (stride) {
    var e;
    for (var i = this.mIndex, c = this.mAnimas, l = c.length; i < l; i++) {
        e = c[i];
        e.update(stride);
        if (e.isFinish()) {
            stride = c.mRemain;
        } else {
            this.mIndex = i;
            return;
        }
    }
    this.mFinish = true;
    this.mRemain = stride;
};
AnimaGroup.prototype.add = function (anima) {
    this.mAnimas.push(anima);
};
AnimaGroup.prototype.remove = function (anima) {
    for (var i = 0, c = this.mAnimas, l = c.length; i < l; i++) {
        if (c[i] === anima) {
            c.splice(i, 1);
            l--;
            i--;
        }
    }
};
/**
 * @param fn remove element if fn return true
 * @param target
 */
AnimaGroup.prototype.removeFn = function (fn, target) {
    target = target || this;
    for (var i = 0, c = this.mAnimas, l = c.length; i < l; i++) {
        if (fn.call(target, c[i])) {
            c.splice(i, 1);
            l--;
            i--;
        }
    }
};
AnimaGroup.prototype.reset = function () {
    for (var i = 0, c = this.mAnimas, l = c.length; i < l; i++) {
        c[i].reset();
    }
    this.mIndex = 0;
    this.mFinish = false;
    this.mRemain = 0;
};

exports.Anima = Anima;
exports.AnimaGroup = AnimaGroup;